"""Build FUSE client"""
import daos_build

def configure_lustre(denv):
    """Do Lustre configure checks"""
    if GetOption('help') or GetOption('clean'):
        return denv
    #If Lustre installed build a Lustre-aware libduns
    conf = Configure(denv)
    gotversion = False
    if not conf.CheckLibWithHeader('lustreapi', 'linux/lustre/lustre_user.h',
                                   'c'):
        print("No installed Lustre version detected")
    else:
        print("Installed Lustre version detected")
        if not conf.CheckFunc('llapi_unlink_foreign'):
            print("Lustre version is not compatible")
        else:
            print("Lustre version is compatible")
            gotversion = True
    if gotversion is True:
        print("Building with Lustre bindings.")
        denv.AppendUnique(CCFLAGS=['-DLUSTRE_INCLUDE'])
    else:
        print("Not building with Lustre bindings.")
    return conf.Finish()

def scons():
    """Execute build"""
    Import('env')

    denv = env.Clone()

    configure_lustre(denv)

    libraries = ['daos_common', 'daos', 'uuid', 'gurt']

    dfs_src = ['dfs.c', 'dfs_sys.c']
    dfs = daos_build.library(denv, 'dfs', dfs_src, LIBS=libraries)
    denv.Install('$PREFIX/lib64/', dfs)

    libraries = libraries + ['dfs']

    denv.AppendUnique(LIBPATH=[Dir('.')])

    dfs_internal_src = ['dfs_internal.c']
    dfs_internal = daos_build.library(denv, 'dfs_internal',
                                      dfs_internal_src, LIBS=libraries)
    denv.Install('$PREFIX/lib64/', dfs_internal)

    duns = daos_build.library(denv, 'duns', 'duns.c', LIBS=libraries)
    denv.Install('$PREFIX/lib64/', duns)

if __name__ == "SCons.Script":
    scons()
